package xyz.xwls.bootadmin.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import xyz.xwls.bootadmin.common.CommonResult;
import xyz.xwls.bootadmin.component.BaseController;
import xyz.xwls.bootadmin.sys.domain.Role;
import xyz.xwls.bootadmin.sys.service.RoleService;
import xyz.xwls.bootadmin.sys.vo.RoleVo;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * @author WZW
 * @date 2020/4/13
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public CommonResult<List<RoleVo>> list(){
        List<RoleVo> roleVoList = roleService.list();
        return CommonResult.success(roleVoList);
    }
    @PostMapping
    public CommonResult<?> save(@RequestBody @Valid Role role, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return getErrors(bindingResult);
        }
        role.setCreateTime(new Date());
        roleService.save(role);
        return CommonResult.success();
    }

    @PutMapping
    public CommonResult<?> update(@RequestBody @Valid Role role, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return getErrors(bindingResult);
        }
        roleService.update(role);
        return CommonResult.success();
    }
}
